package com.architecture.core.web.actions;

import com.architecture.core.web.vo.UserVO;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

/**
 * Created with IntelliJ IDEA.
 * LoginAction: Administrator
 * Date: 12-5-8
 * Time: 下午5:06
 * To change this template use File | Settings | File Templates.
 */
@Controller("loginAction")
@Results({
        @Result(name = "user_login_success", location = "front/login.jsp"),
        @Result(name = "user_login_error", location = "front/login.jsp"),

        @Result(name = "admin_login_success", location = "back/login.jsp"),
        @Result(name = "admin_login_error", location = "back/login.jsp")
})
public class LoginAction extends AbstractAction {

//    @Resource
//    private BaseUserService baseUserService;

    private UserVO userVO = new UserVO();

    private static final String USER_LOGIN_SUCCESS = "user_login_success";
    private static final String USER_LOGIN_ERROR = "user_login_error";
    private static final String ADMIN_LOGIN_SUCCESS = "admin_login_success";
    private static final String ADMIN_LOGIN_ERROR = "admin_login_error";

    @Override
    @Action(value = "login")
    public String execute() {
        return ActionSupport.SUCCESS;
    }

    @Action(value = "user_login")
    public String userLogin() {
//        try {
//
//            //验证验证码是否正确
//
//            //验证用户名密码是否正确
//            boolean isUser = this.baseUserService.checkIsUser(userVO.getUser().getUsername(), userVO.getUser().getPassword(), 2);
//
//            //如果用户不存在则返回无此用户
//            if (false == isUser) {
//                throw new ErrException(ErrCode.NO_USER);
//            }
//
//        } catch (ErrException err) {
//            err.printStackTrace();
//            super.addActionError(err.getMessage());
//            return USER_LOGIN_ERROR;
//        }
        return USER_LOGIN_SUCCESS;
    }

    @Action(value = "admin_login")
    public String adminLogin() {
//        try {
//
//            //验证验证码是否正确
//
//            //验证用户名密码是否正确
//            boolean isUser = this.baseUserService.checkIsUser(userVO.getUser().getUsername(), userVO.getUser().getPassword(), 1);
//
//            //如果用户不存在则返回无此用户
//            if (false == isUser) {
//                throw new ErrException(ErrCode.NO_USER);
//            }
//
//        } catch (ErrException err) {
//            err.printStackTrace();
//            super.addActionError(err.getMessage());
//            return ADMIN_LOGIN_ERROR;
//        }
        return ADMIN_LOGIN_SUCCESS;
    }

    @Override
    public void validate() {
    }

    public UserVO getUserVO() {
        return userVO;
    }

    public void setUserVO(UserVO userVO) {
        this.userVO = userVO;
    }
}
